package com.feeyin.radar.msg.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.msg.dao.InternalMailboxDAO;
import com.feeyin.radar.msg.dao.WarningInfoDAO;
import com.feeyin.radar.msg.dao.WarningInfoObjectDAO;
import com.feeyin.radar.msg.model.WarningInfo;
import com.feeyin.radar.msg.service.WarningInfoService;
import com.feeyin.radar.sys.dao.DataInterfaceDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.ManageSet;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class WarningInfoServiceImpl
  implements WarningInfoService
{
  private WarningInfoDAO warningInfoDAO;
  private WarningInfoObjectDAO warningInfoObjectDAO;
  private ManageSetDAO manageSetDAO;
  private SaleRecordDAO saleRecordDAO;
  private CustomerDAO customerDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private ContractDAO contractDAO;
  private InternalMailboxDAO internalMailboxDAO;
  private DataInterfaceDAO dataInterfaceDAO;

  public WarningInfoDAO getWarningInfoDAO()
  {
    return this.warningInfoDAO;
  }

  public void setWarningInfoDAO(WarningInfoDAO warningInfoDAO) {
    this.warningInfoDAO = warningInfoDAO;
  }

  public WarningInfoObjectDAO getWarningInfoObjectDAO() {
    return this.warningInfoObjectDAO;
  }

  public void setWarningInfoObjectDAO(WarningInfoObjectDAO warningInfoObjectDAO) {
    this.warningInfoObjectDAO = warningInfoObjectDAO;
  }

  public ManageSetDAO getManageSetDAO() {
    return this.manageSetDAO;
  }

  public void setManageSetDAO(ManageSetDAO manageSetDAO) {
    this.manageSetDAO = manageSetDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public ContractDAO getContractDAO() {
    return this.contractDAO;
  }

  public void setContractDAO(ContractDAO contractDAO) {
    this.contractDAO = contractDAO;
  }

  public List getPageScheduleWarningInfosByUserId(Integer userId, Page page) {
    return this.warningInfoDAO.getPageWarningInfosByUserId(userId, page);
  }

  public Integer getPageScheduleWarningInfosCountByUserId(Integer userId) {
    return this.warningInfoDAO.getPageWarningInfosCountByUserId(userId);
  }

  public InternalMailboxDAO getInternalMailboxDAO() {
    return this.internalMailboxDAO;
  }

  public void setInternalMailboxDAO(InternalMailboxDAO internalMailboxDAO) {
    this.internalMailboxDAO = internalMailboxDAO;
  }

  public DataInterfaceDAO getDataInterfaceDAO() {
    return this.dataInterfaceDAO;
  }

  public void setDataInterfaceDAO(DataInterfaceDAO dataInterfaceDAO) {
    this.dataInterfaceDAO = dataInterfaceDAO;
  }

  public List getPageAuditWarningInfosByUserId(Integer userId, Page page, int sign)
  {
    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("auditRemind");
    if (manageSet == null) return null;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return null;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return null;
    if (userIds.indexOf(userId.toString()) <= -1) return null;

    if (userId == null) return null;
    if (page == null) return null;
    if (sign == 0) return null;
    Users user = new Users();
    user = this.userDAO.getUser(userId);
    if (user == null) return null;
    if (user.getDeptId() == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (StringUtils.isEmpty(deptIds)) return null;

    WarningInfo warningInfo = new WarningInfo();
    if (sign == 2) {
      warningInfo.setDeptIds(deptIds);
    }
    warningInfo.setWarningCode("auditRemind");

    List auditWarningInfos = new ArrayList();
    auditWarningInfos = this.warningInfoDAO.getPageWarningInfosByDynamic(warningInfo, page);
    if (auditWarningInfos == null) return null;
    for (int i = 0; i < auditWarningInfos.size(); i++) {
      warningInfo = new WarningInfo();
      warningInfo = (WarningInfo)auditWarningInfos.get(i);
      if (warningInfo.getSponsorId() != null) {
        user = new Users();
        user = this.userDAO.getUser(warningInfo.getSponsorId());
        if (user != null) {
          warningInfo.setSponsor(user.getUserName());
        }
      }
    }
    return auditWarningInfos;
  }

  public Integer getPageAuditWarningInfosCountByUserId(Integer userId, int sign)
  {
    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("auditRemind");
    if (manageSet == null) return Integer.valueOf(0);
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return Integer.valueOf(0);
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return Integer.valueOf(0);
    if (userIds.indexOf(userId.toString()) <= -1) return Integer.valueOf(0);

    if (userId == null) return Integer.valueOf(0);
    if (sign == 0) return Integer.valueOf(0);
    Users user = new Users();
    user = this.userDAO.getUser(userId);
    if (user == null) return Integer.valueOf(0);
    if (user.getDeptId() == null) return Integer.valueOf(0);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (StringUtils.isEmpty(deptIds)) return Integer.valueOf(0);

    WarningInfo warningInfo = new WarningInfo();
    if (sign == 2) {
      warningInfo.setDeptIds(deptIds);
    }
    warningInfo.setWarningCode("auditRemind");
    return this.warningInfoDAO.getPageWarningInfosCountByDynamic(warningInfo);
  }

  public List getPageSystemWarningInfosByUserId(Integer userId, Page page) {
    if (userId == null) return null;
    if (page == null) return null;

    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setWarningSources("3,5");
    warningInfo.setWarningUserId(userId);

    return this.warningInfoDAO.getPageWarningInfosByDynamic(warningInfo, page);
  }

  public Integer getPageSystemWarningInfosCountByUserId(Integer userId) {
    if (userId == null) return null;

    WarningInfo warningInfo = new WarningInfo();
    warningInfo.setWarningSources("3,5");
    warningInfo.setWarningUserId(userId);

    return this.warningInfoDAO.getPageWarningInfosCountByDynamic(warningInfo);
  }

  public boolean insertScheduleWarningInfo() {
    if (!this.manageSetDAO.isStartWarning()) return false;

    boolean result = false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("scheduleRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;

    List saleRecords = new ArrayList();
    saleRecords = this.saleRecordDAO.getSaleRecordsByCreatePersonIds(userIds);
    if ((saleRecords == null) || (saleRecords.size() <= 0)) return false;

    Date curDate = DateUtils.getDateByFormat(new Date(), "yyyy-MM-dd");
    for (int i = 0; i < saleRecords.size(); i++) {
      SaleRecord saleRecord = new SaleRecord();
      saleRecord = (SaleRecord)saleRecords.get(i);
      if (((saleRecord.getContactDate() == null) || (curDate.compareTo(saleRecord.getContactDate()) != 0)) && (
        (saleRecord.getNextContactDate() == null) || (curDate.compareTo(saleRecord.getNextContactDate()) != 0))) continue;
      Customer customer = new Customer();
      customer = this.customerDAO.getCustomerById(saleRecord.getCustomerId());
      if (customer != null) {
        WarningInfo warningInfo = new WarningInfo();
        warningInfo.setForeignId(saleRecord.getId());
        warningInfo.setForeignName(customer.getCustomerName());
        warningInfo.setIsAging("1");
        warningInfo.setIsRead("1");
        warningInfo.setWarningCode("scheduleRemind");
        warningInfo.setWarningDate(new Date());
        warningInfo.setWarningSource("4");
        warningInfo.setWarningUserId(saleRecord.getCreatePersonId());
        if ((curDate.compareTo(saleRecord.getContactDate()) == 0) && 
          (!StringUtils.isEmpty(saleRecord.getContactTime())))
          warningInfo.setAgingDay(Integer.valueOf(Integer.parseInt(saleRecord.getContactTime())));
        else if ((curDate.compareTo(saleRecord.getNextContactDate()) == 0) && 
          (!StringUtils.isEmpty(saleRecord.getNextContactTime()))) {
          warningInfo.setAgingDay(Integer.valueOf(Integer.parseInt(saleRecord.getNextContactTime())));
        }
        this.warningInfoDAO.insertWarningInfo(warningInfo);

        this.internalMailboxDAO.insertInternalMail(warningInfo, 0);

        this.dataInterfaceDAO.insertEMail(warningInfo, 0);
        result = true;
      }
    }

    return result;
  }

  public boolean insertContractOverDueWarningInfo() {
    if (!this.manageSetDAO.isStartWarning()) return false;

    boolean result = false;

    ManageSet manageSet = new ManageSet();
    manageSet = this.manageSetDAO.getManageSetByCode("contractRemind");
    if (manageSet == null) return false;
    if ((StringUtils.isEmpty(manageSet.getStatus())) || (!"1".equals(manageSet.getStatus()))) return false;
    String userIds = StringUtils.getSubLastComma(manageSet.getMValue());
    if (StringUtils.isEmpty(userIds)) return false;

    Contract contract = new Contract();
    contract.setSalePersonIds(userIds);
    contract.setMoreStatus("1,2,3");
    List contracts = new ArrayList();
    contracts = this.contractDAO.getContractsByDynamic(contract);
    if ((contracts == null) || (contracts.size() <= 0)) return false;

    Date curDate = DateUtils.getDateByFormat(new Date(), "yyyy-MM-dd");
    for (int i = 0; i < contracts.size(); i++) {
      contract = new Contract();
      contract = (Contract)contracts.get(i);
      if ((contract.getContractDate() != null) && (curDate.compareTo(contract.getContractDate()) >= 0)) {
        Customer customer = new Customer();
        customer = this.customerDAO.getCustomerById(contract.getCustomerId());
        if (customer != null) {
          WarningInfo warningInfo = new WarningInfo();
          warningInfo.setForeignId(contract.getId());
          warningInfo.setForeignCode(contract.getContractCode());
          warningInfo.setForeignName(customer.getCustomerName());
          warningInfo.setIsAging("2");
          warningInfo.setIsRead("1");
          warningInfo.setWarningCode("contractRemind");
          warningInfo.setWarningDate(new Date());
          warningInfo.setWarningSource("5");
          warningInfo.setWarningUserId(contract.getSalePersonId());
          this.warningInfoDAO.insertWarningInfo(warningInfo);

          this.internalMailboxDAO.insertInternalMail(warningInfo, 1);

          this.dataInterfaceDAO.insertEMail(warningInfo, 1);
          result = true;
        }
      }
    }
    return result;
  }

  public boolean updateWarningInfoForReaded(String[] ids, int sign)
  {
    if ((ids == null) || (ids.length <= 0)) return false;
    for (int i = 0; i < ids.length; i++) {
      WarningInfo warningInfo = new WarningInfo();
      warningInfo.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      warningInfo.setIsRead("2");
      if (sign == 1) {
        warningInfo.setProcessDate(new Date());
      }
      this.warningInfoDAO.updateWarningInfoByDynamic(warningInfo);
    }
    return true;
  }

  public boolean deleteWarningInfo(String[] ids) {
    if ((ids == null) || (ids.length <= 0)) return false;
    for (int i = 0; i < ids.length; i++) {
      this.warningInfoDAO.deleteWarningInfo(Integer.valueOf(Integer.parseInt(ids[i])));
    }
    return true;
  }
}